package cn.wolfcode.web.controller;


import cn.wolfcode.Enum.AppointmentEnums;
import cn.wolfcode.Enum.ConsumptionStatusEnum;
import cn.wolfcode.domain.Consumption;
import cn.wolfcode.domain.ConsumptionItem;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.SystemDictionaryItem;
import cn.wolfcode.service.*;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.util.RequiredPermission;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageRowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/consumption")
public class ConsumptionController {

    @Autowired
    private IConsumptionService consumptionService;

    @Autowired
    private IBusinessService businessService;

    @Autowired
    private IConsumptionItemService consumptionItemService;

    @Autowired
 private ISystemDictionaryItemService systemDictionaryItemService;

    @Autowired
    private IAppointmentService appointmentService;


    @RequiredPermission(name="结算单页面",expermission="consumption:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<Consumption> pageInfo = consumptionService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "consumption/list";
    }


    @RequiredPermission(name="结算单删除",expermission="consumption:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           consumptionService.delete(id);
        }
        return "redirect:/consumption/list";
    }


    @RequiredPermission(name="结算单新增/编辑",expermission="consumption:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Consumption consumption) {
        if (consumption.getId() == null) {
            consumptionService.save(consumption);
        } else {
            consumptionService.update(consumption);
        }
        return "redirect:/consumption/list";
    }


    @RequiredPermission(name="结算单新增/编辑",expermission="consumption:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        model.addAttribute("business",businessService.listAll());
        model.addAttribute("systemDictionaryItems",systemDictionaryItemService.selectBySn("business"));
        model.addAttribute("systemDictionarys",systemDictionaryItemService.selectBySn("pay_type"));
        if (id != null) {
            Consumption consumption = consumptionService.get(id);
            model.addAttribute("consumption",consumption);
            List<ConsumptionItem> items = consumptionItemService.selectBycno(consumption.getCno());
            model.addAttribute("items",items);
        }
        return "consumption/input";
    }
    @RequiredPermission(name="结算单新增",expermission="consumption:save")
    @RequestMapping("/save")
    public String save(Long  appointmentId){
        Consumption consumption = consumptionService.saveFormAppointment(appointmentId);
        return "redirect:/consumption/input?id="+consumption.getId();
    }

    @RequestMapping("/finish")
    public String finish(Consumption consumption){
        if (consumption.getId()!=null){
            Date now = new Date();
            consumption.setPayTime(now);
            Employee employee = UserContext.getCurrentUser();
            consumption.setPayee(employee);
            consumption.setStatus(ConsumptionStatusEnum.AUDIT.getValue());
            consumptionService.updateFinish(consumption);
        }
        return "redirect:/consumption/list";
    }
    @RequestMapping("/asStatus")
    public String saStatus(Consumption consumption){
        if (consumption.getId()!=null){
            Date now = new Date();
            consumption.setAuditTime(now);
            Employee user = UserContext.getCurrentUser();
            consumption.setAuditor(user);
            consumption.setStatus(ConsumptionStatusEnum.FINISH.getValue());
            consumptionService.updateStatus(consumption);
            String customerName = consumption.getCustomerName();
            appointmentService.updateStatuses(customerName, AppointmentEnums.FINISH.getValue());
        }

        return "redirect:/consumption/list";
    }

    @RequestMapping("/account")
    public String account(Consumption consumption){
        if (consumption.getId()!=null){
            Date now = new Date();
            consumption.setAuditTime(now);
            Employee user = UserContext.getCurrentUser();
                consumption.setAuditor(user);
                consumption.setStatus(ConsumptionStatusEnum.FAILURE.getValue());
                consumptionService.updateStatus(consumption);
            String customerName = consumption.getCustomerName();
                appointmentService.updateStatuses(customerName,AppointmentEnums.FAILURE.getValue());
        }
        return "redirect:/consumption/list";
    }


}
